package com.shg.web.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.shg.bean.Ad;
import com.shg.bean.Book;
import com.shg.bean.PageView;
import com.shg.bean.Shop;
import com.shg.service.AdService;
import com.shg.web.tools.BaseConstrant;
import com.shg.web.tools.FreeMarkerTool;
import com.shg.web.tools.WebTool;

@Controller
public class AdAction {

	@Resource	AdService adService;
	
	private int maxrecord=15;
	//
	private int page=1;
	//
	private PageView<Ad> pageView;
	//
	private Map<String, Object> bookMap;
	//
	private String ftlName;
	//
	 private String htmlName;
	 //广告
	 private Ad ad;

	private String message;
	 //广告书籍ID
	private String[] adids;
	//审核类型 1.同意 2.拒绝
	private Integer type;
	//
	private List<Ad> listAd;
	/**
	 *查询用户广告申请
	 * @return
	 */
	public String list(){
		pageView=new PageView<Ad>(maxrecord, page);
		try {
			pageView.setQueryResult(adService.getShopAd(pageView));
		} catch (Exception e) {
			e.printStackTrace();
			this.message="查询用户广告申请产生异常";
			return "error";
		}
		return "list";
	}
	/**
	 * 广告生成
	 * @return
	 */
	public String adMap(){
		try {
			bookMap=adService.getAdList();
			FreeMarkerTool.analysisTemplate(BaseConstrant.FTLPATH, ftlName, BaseConstrant.HTMLPATH,htmlName, bookMap);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="生成静态HTML产生异常";
			return "error";
		}
		return "adMap";
	}
	/**
	 * 查询需要申请首页在首页展示的书籍
	 * @return
	 */
	public String apply(){
		try {
			ad = adService.getApply(ad.getId());
		} catch (Exception e) {
			e.printStackTrace();
			this.message="查询广告书籍的时候产生错误";
			return "error";
		}
		return "apply";
	}
	/**
	 * 店铺广告申请
	 * @return
	 */
	public String shopApply(){
		try {
			SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			this.message=format.format(new Date())+"<br/>"+ad.getWord()+"<br/>"+this.message;
			//店铺
			Shop shop=new Shop();
			shop.setId(WebTool.getLogin().getId());
			ad.setShop(shop);
			//
			Book book=new Book();
			book.setId(ad.getId());
			ad.setBook(book);
			//店铺广告
			ad.setShopad(1);
			ad.setShuhgad(0);
			
			adService.addShopAd(ad);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="保存广告失败";
			return "error";
		}
		return shopAd();
	}
	/**
	 * 店铺广告查询
	 */
	public String shopAd(){
		try {
			listAd = adService.getShop(WebTool.getLogin().getId(),0);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="查询本店的店铺广告发送异常";
			return "error";
		}
		return "shopAd";
	}
	/**
	 * 添加广告 
	 * @return
	 * 格式：
	 * 		年-月-日  时：分：秒
	 * 		描述
	 */
	public String addAd(){
		try {
			SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			this.message=format.format(new Date())+"<br/>"+ad.getWord()+"<br/>"+this.message;
			//店铺
			Shop shop=new Shop();
			shop.setId(WebTool.getLogin().getId());
			ad.setShop(shop);
			ad.setShopad(0);
			ad.setShuhgad(1);
			
			ad.setWord(message);
			adService.addShuhgAd(ad);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="保存广告失败";
			return "error";
		}
		this.message="保存广告成功";
		return "success";
	}
	/**
	 * 审核
	 * @return
	 */
	public String auditing(){
		try {
			adService.updateFlag(adids,type);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="审核产生异常";
			return "error";
		}
		return allList();
	}
	/**
	 * 查询所有的广告
	 * @return
	 */
	public String allList(){
		pageView=new PageView<Ad>(maxrecord, page);
		try {
			pageView.setQueryResult(adService.getAdList(pageView));
		} catch (Exception e) {
			e.printStackTrace();
			this.message="查询所有的广告产生异常";
			return "error";
		}
		return "allList";
	}
	/**
	 * 取消
	 * @return
	 */
	public String cancelAd(){
		try {
			listAd = adService.updateType(ad);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="更新广告发送异常";
			return "error";
		}
		return shopAd();
	}
	public PageView<Ad> getPageView() {
		return pageView;
	}

	public void setPageView(PageView<Ad> pageView) {
		this.pageView = pageView;
	}

	public int getMaxrecord() {
		return maxrecord;
	}

	public void setMaxrecord(int maxrecord) {
		this.maxrecord = maxrecord;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public Map<String, Object> getBookMap() {
		return bookMap;
	}
	public void setBookMap(Map<String, Object> bookMap) {
		this.bookMap = bookMap;
	}
	public String getFtlName() {
		return ftlName;
	}
	public void setFtlName(String ftlName) {
		this.ftlName = ftlName;
	}
	public String getHtmlName() {
		return htmlName;
	}
	public void setHtmlName(String htmlName) {
		this.htmlName = htmlName;
	}
	public Ad getAd() {
		return ad;
	}
	public void setAd(Ad ad) {
		this.ad = ad;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public String[] getAdids() {
		return adids;
	}
	public void setAdids(String[] adids) {
		this.adids = adids;
	}
	public Integer getType() {
		return type;
	}
	public void setType(Integer type) {
		this.type = type;
	}
	public List<Ad> getListAd() {
		return listAd;
	}
	public void setListAd(List<Ad> listAd) {
		this.listAd = listAd;
	}
}
